/**
 * 文件：InsertScorePanel.java
 * 说明：用于添加成绩信息
 * 作者：古通基
 * 时间：2011-09-13
 */

package com.java.component;

import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class InsertScorePanel extends JPanel implements ActionListener,MouseListener,ItemListener
{
	private static final long serialVersionUID = -3348500618040365259L;
	private JLabel lsno = new JLabel("学         号：");                                  //静态标签
	private JLabel lsnmae = new JLabel("姓         名：");
	private JLabel lcno = new JLabel("课程编号：");        
	private JLabel lcname = new JLabel("课  程  名：");      
	private JLabel lgrade = new JLabel("成        绩：");        
	
	private JTextField sno = new JTextField("请输入学号",10);                     //文本输入
	private JTextField sname = new JTextField("",10); 
	private JTextField cno= new JTextField("",10);  
	private JComboBox cname = new JComboBox(); 
    private JTextField grade = new JTextField("",10);  
    
    private JButton insert = new JButton("添加");                                      //声明按钮
    private JButton reset = new JButton("重置");
    private JButton query = new JButton("查询");
    
    private JPanel JPcomon1 = new JPanel();                                            //声明面板
    private JPanel JPcomon2 = new JPanel();
    private JPanel JPcomon3 = new JPanel();
    private JPanel JPcomon4 = new JPanel();
    private JPanel JPcomon5 = new JPanel();
    private JPanel JPbutton = new JPanel();
    
    private String str1[] = new String[20];                                                 //存储选择列表
    private String str2[] = new String[20];
    String tmp = null;
     
    public InsertScorePanel()                                                                     //构造函数
    {
    	lsno.setHorizontalAlignment(SwingConstants.CENTER);
    	lsnmae.setHorizontalAlignment(SwingConstants.CENTER);
    	lcno.setHorizontalAlignment(SwingConstants.CENTER);
    	lcname.setHorizontalAlignment(SwingConstants.CENTER);
    	lgrade.setHorizontalAlignment(SwingConstants.CENTER);
    	   	
    	JPcomon1.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));         //挂载组件
    	JPcomon1.add(lsno);
    	JPcomon1.add(sno);
    	JPcomon1.add(query);
    	JPcomon2.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon2.add(lsnmae);
    	JPcomon2.add(sname);
    	JPcomon3.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon3.add(lcno);
    	JPcomon3.add(cno);
    	JPcomon4.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon4.add(lcname);
    	JPcomon4.add(cname);
    	JPcomon5.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon5.add(lgrade);
    	JPcomon5.add(grade);
    	
    	JPbutton.setLayout(new FlowLayout(FlowLayout.CENTER,12,5));
    	JPbutton.add(insert);
    	JPbutton.add(reset);
    	
    	this.setLayout(new GridLayout(10,1));                                              //挂载面板
    	this.add(JPcomon1);
    	this.add(JPcomon2);
    	this.add(JPcomon3);
    	this.add(JPcomon4);
    	this.add(JPcomon5);
    	this.add(JPbutton);
    	
    	sname.setEnabled(false);
    	cno.setEnabled(false);
    	
    	insert.addActionListener(this);                                                          //事件监听
    	query.addActionListener(this);
    	reset.addActionListener(this);
    	sno.addMouseListener(this);
    }    
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand().equals("添加"))
		{
			AddToDataBase();
			query.setEnabled(true);
		}
		else if(e.getActionCommand().equals("查询"))
		{
			QueryDataBase();
			query.setEnabled(false);
			sname.setEnabled(false);
			sno.setEnabled(false);
			cname.addItemListener(this);
		}
		else if(e.getActionCommand().equals("重置"))
		{
			sno.setText("");
			sno.setEnabled(true);
			sname.setText("");			
			cno.setText("");
			grade.setText("");
			cname.removeAllItems();
			query.setEnabled(true);
		}
	}
	private void QueryDataBase()
	{		
		ResultSet rs = null;
		try
		{
			BaseSQL score = null;
			score = new BaseSQL();
			String sql = "select sno,sname from student where sno = '"+Integer.parseInt(sno.getText())+"'" ; 			
		    rs = score.executeSelect(sql);		                                               //查询学生          
		    if (rs.next())
    		{
		    	if(Integer.parseInt(sno.getText())==rs.getInt("sno"))
		    	{
		    		sname.setText(rs.getString("sname"));;
		    	}
    		} 
		    else
	    	{
	    		JOptionPane.showMessageDialog(this, "不存在学号为："+sno.getText()+" 的学生", "查询错误",JOptionPane.INFORMATION_MESSAGE);
	    	}
		    rs.close();
		    rs = score.executeSelect("select cno,cname from course");         //查询课程
		    int i = -1;
		    for (i= 0;rs.next();i++)
    		{
		    	cname.addItem(rs.getString("cname"));
		    	str1[i] = rs.getString("cname");
		    	cno.setText(rs.getString("cno"));
    			str2[i]=rs.getString("cno");
    		} 	
		    cname.setSelectedIndex(i-1);
		    rs.close();
		}
		catch(SQLException evt)
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		} 
		catch (ClassNotFoundException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
		catch (NumberFormatException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}	
	}	
	private void AddToDataBase()
	{	
		try
		{
			BaseSQL score = null;
			score = new BaseSQL();
			String sql = "insert into sc values('"+Integer.parseInt(sno.getText())+"','"+Integer.parseInt(cno.getText())+"','"+Integer.parseInt(grade.getText())+"','"+sname.getText()+"','"+tmp+"')";
		    score.executeDML(sql);
		    JOptionPane.showMessageDialog(this, "添加成功", "添加成绩",JOptionPane.INFORMATION_MESSAGE);
		}
		catch(SQLException evt)
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		} 
		catch (ClassNotFoundException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
		catch (NumberFormatException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
	}

	public void itemStateChanged(ItemEvent e)
	{
		if(e.getStateChange()==ItemEvent.SELECTED)
		{
			int i = cname.getItemCount(); 
			if(e.getSource() == cname ) 
			{
				for(;i>=0;i--)
				{
					if(e.getItem().equals(str1[i]))
					{
						cno.setText(str2[i]);
						tmp = (String) cname.getSelectedItem();
					}
			     }  
			}
		}			
	}
	
    public void mouseClicked(MouseEvent e) { }                                //鼠标事件
    public void mouseReleased(MouseEvent e) { }
    public void mouseEntered(MouseEvent e) 
    {
    	if(sno.getText().equals("请输入学号"))
    	{    		
    		sno.setText("");
    		this.setVisible(true);
    	}
    	else if(grade.getText().equals("成绩"))
    	{
    		grade.setText("");
    		this.setVisible(true);
    	}    	
    }
    public void mousePressed(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }
}
